package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsStory;
import com.zjxf.base.bean.ro.RoCmsStory;
import com.zjxf.base.bean.vo.VoCmsStory;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.service.CmsStoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 长征故事
 *
 * @author ZhangWei
 * @date 2021/4/6/006 19:13
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "长征故事")
@RequestMapping("/cmsLmStory/")
public class CmsLmStoryController {

    private final CmsStoryService cmsStoryService;

    @ApiOperation(value = "添加或修改")
    @PostMapping(value = "addOrUpdate")
    public JSONResponse addOrUpdate(@Valid @RequestBody VoCmsStory.addOrUpdate vo) {
        cmsStoryService.addOrUpdate(vo);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public JSONResponse delete(@NotNull(message = "请填写故事id") @RequestParam("id") Long id) throws OperationException {
        cmsStoryService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @GetMapping("get")
    public JSONResponse get(@NotNull(message = "请填写故事id") @RequestParam("id") Long id) throws OperationException {
        CmsStory cmsStory = cmsStoryService.getEntity(id);
        RoCmsStory.Details roLmStory = cmsStoryService.getEntityRo(cmsStory);
        return JSONResponse.success(roLmStory);
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public JSONResponse page(@Valid @RequestBody VoCmsStory.filter filter) {
        Page<CmsStory> page = cmsStoryService.page(filter);
        JSONObject jsonData = cmsStoryService.pageRo(page);
        return JSONResponse.success(jsonData);
    }
}
